<?php
namespace Admin\Model;
use Think\Model;
class QyServiceModel extends Model {

	/**增加服务操作*/
	public function add_service( $add_data ){
		$r = array('d'=>NULL,'i'=>'','r'=>0);
		if( !$add_data['name'] || !$add_data['type'] )
			$r['i'] = "表单信息获取失败";
		else{
			$d = array();
			$d['name'] = $add_data['name'];
			$d['type'] = $add_data['type'];
			$d['remark'] = $add_data['remark'];
			$addID = $this->add( $d );
			if($addID){
				$r['r'] = 1;
				$r['d'] = $addID;
				$r['i'] = 'ok';
			}else{
				$r['i'] = 'fail';
			}
		}
		return $r;
	}

	/** 更新服务操作*/
	public function edit_service($edit_data){
		$r = array('r'=>0,'d'=>NULL,'m'=>'OK' );
		$wheres = array();
		$wheres['service_id'] = array('eq', $edit_data['service_id'] );
		$dproduct = $this->where( $wheres )->find();
		if(!$dproduct){
			$r['i'] = "产品信息获取失败";
			return $r;
		}else{
			$d['name'] = $edit_data['name'];
			$d['type'] = $edit_data['type'];
			$d['remark'] = $edit_data['remark'];
			$editOrNot = $this->where( $d )->find();
			if(!$editOrNot){	//修改了服务信息
				$editID = $this->where( $wheres )->save( $d );
				if($editID){
					$r['r'] = 1;
					$r['d'] = $this->where( $wheres )->find();
					$r['i'] = 'OK';
				}else{
					$r['i'] = '编辑服务失败';
				}
			}else{
				$r['r'] = 2;	//未修改服务信息
			}
		}
		return $r;
	}
	
	/** 删除服务操作*/
	public function delete_service( $service_id ){
		$r = array('r'=>0,'i'=>'server error','d'=>NULL );
		if( !$service_id ){
			$r['i'] = '获取表单参数错误！';
			return $r;
		}
		$wheres = array();
		$wheres['qy_service.service_id'] = array('eq', $service_id);

		$dlist1 = M('qy_service')
			  ->join('package_service  as  b  on  qy_service.service_id = b.service_id')
			  ->where( $wheres )
			  ->select();
		if($dlist1){
			$r['r'] = 2;
			$r['i'] = "该服务存在关联的套餐，暂时无法删除！";
		}
		if(!$dlist1){
			$where = array();
			$where['service_id'] = array('eq', $service_id);
			$d = array();
			$d['is_deleted'] = 1;
			$delID = $this->where( $where )->save($d);
			if($delID){
				$r['r'] = 1;
				$r['i'] = 'OK';
			}else{
				$r['i'] = '删除失败！';
			}
		}
 		return $r;
	}

}